//
//  CSSStatusRetweetView.swift
//  weibo28
//
//  Created by chushushan on 16/5/14.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SnapKit



class CSSStatusRetweetView: UIView {
    
    //记录视图的地步约束
    var retweetViewBottom: Constraint?

    var statusViewModel: CSSStatusViewModel? {
    
        didSet {
        
            contentLabel.text = statusViewModel?.retweetContent
            
//            pictureView.picUrls = statusViewModel?.status?.pic_urls
            //销毁约束
            retweetViewBottom?.uninstall()
            //判断是否有配图
            if let picUrls = statusViewModel?.status?.retweeted_status?.pic_urls where picUrls.count > 0 {
                
                pictureView.picUrls = picUrls
            //配图view显示
                pictureView.hidden = false
                //更新约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    
                    self.retweetViewBottom = make.bottom.equalTo(pictureView).offset(StatusTableViewCellMargin).constraint
                })
                
                
            } else {
            
            //配图view隐藏
                pictureView.hidden = true
            //更新约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    
                    self.retweetViewBottom = make.bottom.equalTo(contentLabel).offset(StatusTableViewCellMargin).constraint
                })

            }
            
        }
    
    }
    
    //转发微博内容
    private lazy var contentLabel: UILabel = {
    
        let label = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 14)
        
        label.numberOfLines = 0
        label.text = "哈哈哈"
        return label
    }()
    
    //转发微博配图
    private lazy var pictureView: CSSStatusPictureView = {
        
        let view = CSSStatusPictureView()
        
        view.backgroundColor = self.backgroundColor
        return view
    }()
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    

    private func setupUI() {
    
        self.backgroundColor = UIColor(white: 0.95, alpha: 1)
        //添加控件
        addSubview(contentLabel)
        
        addSubview(pictureView)
        
        
        
        
        //label的约束
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.left.equalTo(self).offset(StatusTableViewCellMargin)
            make.right.equalTo(self).offset(-StatusTableViewCellMargin)
            
        }
        
        //配图约束
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLabel.snp_bottom).offset(StatusTableViewCellMargin)
            make.left.equalTo(contentLabel)
            make.size.equalTo(CGSize(width: 100, height: 100))
        }
        
        
        
        //当前视图的约束
        self.snp_makeConstraints { (make) -> Void in
           self.retweetViewBottom = make.bottom.equalTo(pictureView).offset(StatusTableViewCellMargin).constraint
        }
    
    }
   
}















